package mk.com.ldm.corepoint.implementation.client.lib.wrappers.filters;

import java.util.HashMap;

import mk.com.ldm.corepoint.implementation.client.lib.generators.BaseFilter;
import mk.com.ldm.corepoint.implementation.shared.Filter;
import mk.com.ldm.corepoint.implementation.shared.TableColumn;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TextBoxFilter extends BaseFilter{

	public TextBoxFilter(final HashMap<String, Filter> filters, final TableColumn tc, final Button filterBtn) {
		super(filters, tc, filterBtn);
		VerticalPanel hp = new VerticalPanel();
		final TextBox tb = new TextBox();
		tb.setWidth(tc.getWidth()-2+"px");
		tb.addStyleName("text-box-filter");
		tb.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				if(!tb.getValue().isEmpty()){
					HashMap<String, String> hm = new HashMap<String, String>();
					hm.put(tb.getName(), tb.getValue());
					Filter f = new Filter(tc.getHeader(), hm, tc.getFilterOperator());
					filters.put(tc.getHeader(), f);
				}
				else{
					filters.remove(tc.getHeader());
				}
			}
		});
		tb.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if(event.getNativeKeyCode()==13){
					filterBtn.click();
				}
			}
		});
		hp.add(tb);
		
		initWidget(hp);
	}
	
}
